﻿﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PT.Common
{
    /// <summary>
    /// 主机配置获取类
    /// 提供从配置文件中读取配置项的方法
    /// </summary>
    public static class getHostConfig
    {
        /// <summary>
        /// 获取INI配置文件路径
        /// </summary>
        private static string INIConfigFilePath => Path.Combine(AppContext.BaseDirectory, "YSConfig.ini");

        /// <summary>
        /// 获取INI配置文件操作对象
        /// </summary>
        private static PT.Common.IniConfig GetIniConfig()
        {
            return new PT.Common.IniConfig(INIConfigFilePath);
        }

        private static string XMLConfigFilePath => Path.Combine(AppContext.BaseDirectory, "YSConfig.xml");
        private static PT.Common.XmlConfig GetXMLConfig()
        {
            return new PT.Common.XmlConfig(XMLConfigFilePath);
        }

        /// <summary>
        /// 从配置文件读取本机名
        /// </summary>
        public static string hostName
        {
            get
            {

                string rehostName = "";

                //从配置文件加载- 使用跨平台路径
                string FilePath = Path.Combine(AppContext.BaseDirectory, "YSConfig.ini");

                Common.IniConfig myINIConfig = new Common.IniConfig(FilePath);

                //加载配置文件
                string strHostName = "";
                myINIConfig.ReadConfig("本地配置", "本机名", ref strHostName);
                if (strHostName != null && strHostName != "")
                {
                    rehostName = strHostName;
                }

                return rehostName;
            }
        }

        /// <summary>
        /// 获取redis直连连接字符串
        /// </summary>
        public static string redisConnectString
        {
            get
            {
                string reConn = "";
                string redisConnectString = "";
                string redisDESEncrypt = "";

                string FilePath = Path.Combine(AppContext.BaseDirectory, "YSConfig.xml");
                PT.Common.XmlConfig myXmlConfig = new PT.Common.XmlConfig(FilePath);

                myXmlConfig.ReadConfig("/configuration/redisDatabase/DESEncrypt", ref redisDESEncrypt);
                myXmlConfig.ReadConfig("/configuration/redisDatabase/connectString", ref redisConnectString);
                if (redisDESEncrypt == "true")
                {
                    reConn = PT.Common.DESEncrypt.Decrypt(redisConnectString);
                    if (reConn == redisConnectString)
                    {
                        Console.WriteLine($"解密配置信息失败: {redisConnectString}");
                    }
                    else
                    {
                        Console.WriteLine($"解密配置信息成功");
                        //Console.WriteLine($"解密配置信息成功: {reConn}");
                    }
                }

                return reConn;
            }
        }

        /// <summary>
        /// 是否使用哨兵模式
        /// </summary>
        public static string redisConnectString_isSentinel
        {
            get
            {
                string isSentinel = "";
                GetXMLConfig().ReadConfig("/configuration/redisDatabase/isSentinel", ref isSentinel);
                return isSentinel;
            }
        }

        /// <summary>
        /// 哨兵模式下的主节点名称
        /// </summary>
        public static string redisConnectString_mastername
        {
            get
            {
                string mastername = "";
                GetXMLConfig().ReadConfig("/configuration/redisDatabase/mastername", ref mastername);
                return mastername;
            }
        }
        /// <summary>
        /// 哨兵模式下的密码
        /// </summary>
        public static string redisConnectString_passwd
        {
            get
            {
                string passwd = "";
                GetXMLConfig().ReadConfig("/configuration/redisDatabase/passwd", ref passwd);
                return passwd;
            }
        }
        /// <summary>
        /// 哨兵模式下的哨兵节点列表
        /// </summary>
        public static Hashtable redisConnectString_SentinelNodeList
        {
            get
            {
                Hashtable settings = new Hashtable();
                GetXMLConfig().ReadConfigList("/configuration/redisDatabase/SentinelNodeList", ref settings);
                return settings;
            }
        }


        /// <summary>
        /// 消息发送API接口
        /// </summary>
        public static string MessageAPIUrl
        {
            get
            {
                string url = "";
                GetXMLConfig().ReadConfig("/configuration/MessageAPIUrl", ref url);
                return url;
            }
        }

        /// <summary>
        /// 魔搭社区的apikey
        /// </summary>
        public static string ModelScopeApiKey
        {
            get
            {
                string url = "";
                GetXMLConfig().ReadConfig("/configuration/ModelScopeApiKey", ref url);
                return url;
            }
        }

        /// <summary>
        /// 微信云托管消息发送API接口
        /// </summary>
        public static string WeiXinCloudAPIUrl
        {
            get
            {
                string url = "";
                GetXMLConfig().ReadConfig("/configuration/WeiXinCloudAPIUrl", ref url);
                return url;
            }
        }

        

        

    } 
}